package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    IEmployeeService employeeService;

    @RequestMapping("")
    @RequiresPermissions(value = {"员工首页","employee:index"} ,logical = Logical.OR)
    public String index(){

        return "employee/list";
    }

    @RequestMapping("list")
    @RequiresPermissions(value = {"员工列表","employee:list"} ,logical = Logical.OR)
    @ResponseBody
    public PageResult list(EmployeeQueryObject qo){

        //这里实现不同用户拥有的不同角色和权限,可以进行不同的操作


        PageResult result = employeeService.query(qo);
        return result;
    }

    @RequestMapping("listData")
    @ResponseBody
    public Object listData(){
        return employeeService.list() ;
    }

    @RequestMapping("save")
    @RequiresPermissions(value = {"员工保存","employee:save"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult save(Employee entity){
        JSONResult result = new JSONResult();
        try{
            employeeService.save(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"员工更新","employee:update"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult update(Employee entity){
        JSONResult result = new JSONResult();
        try{
            employeeService.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }


    @RequestMapping("delete")
    @RequiresPermissions(value = {"员工删除","employee:delete"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            employeeService.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("changeState")
    @RequiresPermissions(value = {"改变员工状态","employee:changeState"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult changeState(Long id){
        JSONResult result = new JSONResult();
        try{
            employeeService.changeState(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }
}
